<HTML>
<HEAD>
<TITLE>[Chapter 5] 5.2 Labels</TITLE>
<META NAME="author" CONTENT="John Zukowski">
<META NAME="date" CONTENT="Thu Jul 31 14:34:25 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java AWT">
<META NAME="title" CONTENT="Java AWT">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java AWT" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch05_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 5<br>Components</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch05_03.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JAWT-CH-5-SECT-2">5.2 Labels</A></h2>

<P CLASS=para>
<A NAME="CH05.LABEL"></A>Having covered the features of the <tt CLASS=literal>Component</tt> 
class, we can now look at some of the simplest components. The first component 
introduced here is a <tt CLASS=literal>Label</tt>. 
A label is a <tt CLASS=literal>Component</tt> that 
displays a single line of static text.[3] 
It is useful for putting a title or message next to another component. 
The text can be centered or justified to the left or right. Labels react 
to all events they receive. However, they do not get any events from their 
peers. 

<blockquote class=footnote>
<P CLASS=para>[3] 
 
<I CLASS=emphasis>Java in A Nutshell</I> 
(from O'Reilly &amp; Associates) includes a multiline <tt CLASS=literal>Label</tt> component.
</blockquote>
<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-5-SECT-2.1">Label Methods</A></h3>Constants

<P CLASS=para>
There are three alignment specifiers for labels. The alignment tells the 
<tt CLASS=literal>Label</tt> where to position its 
text within the space allotted. Setting an alignment for a <tt CLASS=literal>Label</tt> 
might not do anything noticeable if the <tt CLASS=literal>LayoutManager</tt> 
being used does not resize the <tt CLASS=literal>Label</tt> 
to give it more space. With <tt CLASS=literal>FlowLayout</tt>, 
the alignment is barely noticeable. See <A HREF="ch07_01.htm">Chapter 7, <i>Layouts</i></A>, for 
more information. 

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public final static int LEFT </I><br>
<DD>

<P CLASS=para>
<tt CLASS=literal>LEFT</tt> is the constant for left 
alignment. If no alignment is specified in the constructor, left alignment 
is the default. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public final static int CENTER </I><br>
<DD>

<P CLASS=para>
<tt CLASS=literal>CENTER</tt> is the constant for 
center alignment. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public final static int RIGHT </I><br>
<DD>

<P CLASS=para>
<tt CLASS=literal>RIGHT</tt> is the constant for right 
alignment. </DL>
Constructors

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public Label () </I><br>
<DD>

<P CLASS=para>
This constructor creates an empty <tt CLASS=literal>Label</tt>. 
By default, the label's text is left justified. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public Label (String label) </I><br>
<DD>

<P CLASS=para>
This constructor creates a <tt CLASS=literal>Label</tt> 
whose initial text is <tt CLASS=literal>label</tt>. 
By default, the label's text is left justified. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public Label (String label, int alignment) </I><br>
<DD>

<P CLASS=para>
This constructor creates a <tt CLASS=literal>Label</tt> 
whose initial text is <tt CLASS=literal>label</tt>. 
The alignment of the label is <tt CLASS=literal>alignment</tt>. 
If <tt CLASS=literal>alignment</tt> is invalid (not 
<tt CLASS=literal>LEFT</tt>, <tt CLASS=literal>RIGHT</tt>, 
or <tt CLASS=literal>CENTER</tt>), the constructor 
throws the run-time exception <tt CLASS=literal>IllegalArgumentException</tt>. </DL>
Text

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public String getText () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getText()</tt> method returns 
the current value of <tt CLASS=literal>Label</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public void setText (String label) </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>setText()</tt> method changes 
the text of the <tt CLASS=literal>Label</tt> to <tt CLASS=literal>label</tt>. 
If the new label is a different size from the old one, you should revalidate 
the display to ensure the label's entire contents will be seen. </DL>
Alignment

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public int getAlignment () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getAlignment()</tt> method returns 
the current alignment of the <tt CLASS=literal>Label</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public void setAlignment (int alignment) </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>setAlignment()</tt> method changes 
the alignment of the <tt CLASS=literal>Label</tt> 
to <tt CLASS=literal>alignment</tt>. If <tt CLASS=literal>alignment</tt> 
is invalid (not <tt CLASS=literal>LEFT</tt>, <tt CLASS=literal>RIGHT</tt>, 
or <tt CLASS=literal>CENTER</tt>), <tt CLASS=literal>setAlignment()</tt> 
throws the run-time exception <tt CLASS=literal>IllegalArgumentException</tt>. 
<A HREF="ch05_02.htm#JAWT-CH-5-FIG-2">Figure 5.2</A> shows all three alignments. </DL>
<DIV CLASS=figure>
<h4 CLASS=figure><A CLASS="TITLE" NAME="JAWT-CH-5-FIG-2">Figure 5.2: Labels with different alignments</A></h4>


<p>
<img align=middle src="./figs/jawt0502.gif" alt="[Graphic: Figure 5-2]" width=378 height=202 border=0>

</DIV>

Miscellaneous methods

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public synchronized void addNotify () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>addNotify()</tt> method creates 
the <tt CLASS=literal>Label</tt> peer. If 
you override this method, first call <tt CLASS=literal>super.addNotify()</tt>, 
then put in your customizations. Then you will be able to do everything 
you need with the information about the newly created peer. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>protected String paramString () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>paramString()</tt> method overrides 
<tt CLASS=literal>Component</tt>'s <tt CLASS=literal>paramString()</tt> 
method. It is a protected method that calls the overridden <tt CLASS=literal>paramString()</tt> 
to build a <tt CLASS=literal>String</tt> from the 
different parameters of the <tt CLASS=literal>Component</tt>. 
When the method <tt CLASS=literal>paramString()</tt> is called for a <tt CLASS=literal>Label</tt>, the 
alignment and label's text are added. Thus, for the <tt CLASS=literal>Label</tt> 
created by the constructor <tt CLASS=literal>new Label (`ZapfDingbats`, 
Label.RIGHT)</tt>, the results displayed from a call 
to <tt CLASS=literal>toString()</tt> would be: </DL>
<DIV CLASS=screen>
<P>
<PRE>
java.awt.Label[0,0,0x0,invalid,align=right,label=ZapfDingbats]
</PRE>
</DIV>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-5-SECT-2.2">Label Events</A></h3>

<P CLASS=para>
The <tt CLASS=literal>Label</tt> component can react 
to any event it receives, though the <tt CLASS=literal>Label</tt> 
peer normally does not send any. However, there is nothing to stop you 
from posting an event yourself. 

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch05_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch05_03.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Component</td>
<td width=171 align=center valign=top><a href="index/idx_a.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Buttons</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
